﻿using GalaSoft.MvvmLight.Messaging;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MVBuddy.Messages;
using MVBuddy.ViewModel;
using System;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace MVBuddy.View
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            Messenger.Default.Register<CredentialsStoredMessage>(this, CredentialsStoredMessageReceived);
            Messenger.Default.Send<CheckCredentialsStoredMessage>(new CheckCredentialsStoredMessage());

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            Messenger.Default.Unregister<CredentialsStoredMessage>(this, CredentialsStoredMessageReceived);

            base.OnNavigatedFrom(e);
        }

        private void Login_Click(object sender, System.EventArgs e)
        {
            this.ProceedToLogin();
        }

        private void CredentialsStoredMessageReceived(CredentialsStoredMessage message)
        {
            this.ProceedToLogin();
        }

        private void ProceedToLogin()
        {
            NavigationService.Navigate(new Uri("/View/InfoPage.xaml", UriKind.Relative));
            NavigationService.RemoveBackEntry();
        }
    }
}